package org.jb.sharedbills.server;

import com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.SerializationException;
import com.google.gwt.user.server.rpc.RPC;
import com.google.gwt.user.server.rpc.RPCRequest;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/*
 * Copyright 2010 - Jeremie Brebec
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
public abstract class BaseRPCRemoteServiceServlet extends RemoteServiceServlet {
	  /**
     * 
     */
    private static final long serialVersionUID = -1999029484290937757L;

	@Override
	public String processCall(String payload) throws SerializationException {
	      
		try {
		  RPCRequest req = RPC.decodeRequest(payload, null, this);
		
		  RemoteService service = getServiceInstance(
		        req.getMethod().getDeclaringClass());
		
		  return RPC.invokeAndEncodeResponse(service, req.getMethod(),
		    req.getParameters(), req.getSerializationPolicy());
		} catch (IncompatibleRemoteServiceException ex) {
		  log("IncompatibleRemoteServiceException in the processCall(String) method.",
		      ex);
		  return RPC.encodeResponseForFailure(null, ex);
		}
	}
	
	@SuppressWarnings({"unchecked"})
	abstract protected RemoteService getServiceInstance(Class serviceClass);
}